import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    inputvalue: '123',
    statt: 5,
    select: 1
  },
  mutations: {
    lisinit (state, list) {
      state.list = list
    },
    setinputvalue (state, value) {
      state.inputvalue = value
    },
    additem (state) {
      const obj = {
        id: state.statt,
        done: false,
        info: state.inputvalue

      }
      state.list.push(obj)
      state.statt++
      state.inputvalue = ''
    },
    remove (state, id) {
      var index = state.list.findIndex(item => item.id === id)
      state.list.splice(index, 1)
    },
    changgered (state, id) {
      var index = state.list.findIndex(item => item.id === id)
      state.list[index].done = !state.list[index].done
    },
    djchangge (state, id) {
      state.select = id
    },
    clearlist (state) {
      state.list = state.list.filter(item => item.done === false)
    }
  },
  actions: {
    getlist (context) {
      axios.get('list.json', {
        params: {}
      }).then((ret) => {
        context.commit('lisinit', ret.data)
        console.log(ret.data)
      })
    }

  },
  getters: {
    leftNuber (state) {
      return state.list.filter(item => item.done === false).length
    }
    // slectliste (state) {
    //   if (state.list.select === 1) {
    //     return state.list
    //   } else if (state.list.select === 2) {
    //     return state.list.filter(item => item.done === false)
    //   } else {
    //     return state.list.filter(item => item.done === true)
    //   }
    // }

  },
  modules: {
  }
})
